import json
import datetime
from django.http import HttpResponse
from app.models import *



# 删除角色信息
def deleteRole(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
     # 获取唯一标识符，此处为position
    role = data.get('role', None)
    context = dict()
    try:
        # 调用删除函数
        Role.objects.filter(role=role).delete()
        # 用户删除成功
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 删除用户信息
def deleteUser(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
     # 获取唯一标识符，此处为position
    email = data.get('email', None)
    context = dict()
    try:
        # 调用删除函数
        User.objects.filter(email=email).delete()
        # 用户删除成功
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 删除仓库信息
def deleteWarehouse(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
     # 获取唯一标识符，此处为position
    code = data.get('code', None)
    context = dict()
    try:
        # 调用删除函数
        Warehouse.objects.filter(code=code).delete()
        # 用户删除成功
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 删除物料信息
def deleteMaterial(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
     # 获取唯一标识符，此处为position
    code = data.get('code', None)
    context = dict()
    try:
        # 调用删除函数
        Material.objects.filter(code=code).delete()
        # 用户删除成功
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 删除部门信息
def deleteDepartment(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
     # 获取唯一标识符，此处为position
    name = data.get('name', None)
    context = dict()
    try:
        # 调用删除函数
        Department.objects.filter(name=name).delete()
        # 用户删除成功
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 删除物料类别信息
def deleteMaterialType(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
     # 获取唯一标识符，此处为position
    type_name = data.get('type_name', None)
    context = dict()
    try:
        # 调用删除函数
        MaterialType.objects.filter(type_name=type_name).delete()
        # 用户删除成功
        context['result'] = 1
    except:
        context['result'] = 0
    return context